DEPLOY_PATH=./../../_deployment
BIN_PATH=./../../bin
TMP_PATH=./../../tmp
SRC_PATH=./../../src
SRC_BLUA_PATH=$(SRC_PATH)/beyondlua
SRC_LUA_PATH=$(SRC_BLUA_PATH)/lua

LUA_C=lapi.c lauxlib.c lbaselib.c lcode.c ldblib.c ldebug.c ldo.c ldump.c lfunc.c lgc.c linit.c liolib.c llex.c lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c loslib.c lparser.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c ltm.c lundump.c lvm.c lzio.c print.c

BLUA_C=bl_main.c blua.c

LUA_O=$(LUA_C:%.c=%.o)
BLUA_O=$(BLUA_C:%.c=%.o)
LUA_D=$(LUA_C:%.c=%.d)
BLUA_D=$(BLUA_C:%.c=%.d)

$(BIN_PATH)/main:$(LUA_O:%.o=$(TMP_PATH)/%.o) $(BLUA_O:%.o=$(TMP_PATH)/%.o)
	gcc -lm -o $(BIN_PATH)/main $(LUA_O:%.o=$(TMP_PATH)/%.o) $(BLUA_O:%.o=$(TMP_PATH)/%.o)

$(TMP_PATH)/%.o:$(SRC_BLUA_PATH)/%.c
	gcc -c $< -o $@

$(TMP_PATH)/%.o:$(SRC_LUA_PATH)/%.c
	gcc -c $< -o $@

$(TMP_PATH)/%.d:$(SRC_BLUA_PATH)/%.c
	@gcc -MM $< > $@.tmp
	@sed 's,\(.*.o\)\(:\),$@ $(TMP_PATH)/\1\2,g' $@.tmp > $@

$(TMP_PATH)/%.d:$(SRC_LUA_PATH)/%.c
	@gcc -MM $< > $@.tmp
	@sed 's,\(.*.o\)\(:\),$@ $(TMP_PATH)/\1\2,g' $@.tmp > $@

sinclude $(LUA_D:%.d=$(TMP_PATH)/%.d) $(BLUA_D:%.d=$(TMP_PATH)/%.d)

clean:
	rm -f $(BIN_PATH)/*
	rm -f $(TMP_PATH)/*

deploy:
	cp $(BIN_PATH)/main $(DEPLOY_PATH)/main
